package com.hiwan.unions.view.popupWindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.hiwan.unions.R;
import com.hiwan.unions.adapter.bean.MembersAdresssBean;
import com.pl.wheelview.WheelView;

import java.util.ArrayList;

/**
 * Created by llg on 2017/7/9.
 */
public class MembersListPop extends PopupWindow {
    private Activity activity;
    private View conentView;
    public OnTextListLinstener cardListener;
    private WheelView wheel1, wheel2;
    private int i1 = 0, i2 = 0;
    private ArrayList<MembersAdresssBean> arry_list_z = new ArrayList<MembersAdresssBean>();
    private ArrayList<MembersAdresssBean> arry_years = new ArrayList<MembersAdresssBean>();
    private ArrayList<MembersAdresssBean> arry_list2 = new ArrayList<MembersAdresssBean>();
    private ArrayList<String> arry_years1 = new ArrayList<String>();
    private ArrayList<String> arry_years2 = new ArrayList<String>();

    public MembersListPop(final Activity context, final ArrayList<MembersAdresssBean> list) {
        this.activity = context;
        this.arry_list_z = list;
        DisplayMetrics dm = new DisplayMetrics();
        //获取屏幕信息
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        conentView = inflater.inflate(R.layout.popwindow_data_list, null);
        // 设置SelectPicPopupWindow的View
        this.setContentView(conentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 刷新状态
        this.update();
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setAnimationStyle(R.style.mypopwindow_anim_style);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        conentView.setFocusable(true);
        conentView.setFocusableInTouchMode(true);
        conentView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK)
                    dismiss();
                return false;
            }
        });
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams params = activity.getWindow().getAttributes();
                params.alpha = 1f;
                activity.getWindow().setAttributes(params);
            }
        });
        wheel1 = findView(conentView, R.id.wheel1);
        wheel1.setCyclic(false);
        wheel2 = findView(conentView, R.id.wheel2);
        wheel2.setCyclic(false);
        arry_years = stringArrayList(list);
        wheel1.setData(stringArrayListString(arry_years));
        wheel2.setData(stringArrayListString2(list, arry_years.get(0).getArea_code()));
        findView(conentView, R.id.disms).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        findView(conentView, R.id.custom).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cardListener.OnDateText(arry_years.get(i1).getArea_code(), arry_years.get(i1).getArea_name(), arry_list2.get(i2).getArea_code(), arry_list2.get(i2).getArea_name());
                dismiss();
            }
        });
        wheel1.setOnSelectListener(new WheelView.OnSelectListener() {
            @Override
            public void endSelect(int id, String text) {
                i1 = id;
                i2 = 0;
                wheel2.setDefault(i2);
                wheel2.refreshData(stringArrayListString2(list, arry_years.get(id).getArea_code()));

            }

            @Override
            public void selecting(int id, String text) {
            }
        });
        wheel2.setOnSelectListener(new WheelView.OnSelectListener() {
            @Override
            public void endSelect(int id, String text) {
                i2 = id;
            }

            @Override
            public void selecting(int id, String text) {
            }
        });
    }

    private ArrayList<String> stringArrayListString2(ArrayList<MembersAdresssBean> strings, String code) {
        arry_years2.clear();
        arry_list2.clear();
        for (MembersAdresssBean s : strings) {
            if (s.getParent_area_code().equals(code)) {
                arry_list2.add(s);
                arry_years2.add(s.getArea_name());
            }
        }
        return arry_years2;
    }

    private ArrayList<String> stringArrayListString(ArrayList<MembersAdresssBean> strings) {
        arry_years1.clear();
        for (MembersAdresssBean s : strings) {
            arry_years1.add(s.getArea_name());
        }
        return arry_years1;
    }

    private ArrayList<MembersAdresssBean> stringArrayList(ArrayList<MembersAdresssBean> strings) {
        arry_years.clear();
        for (MembersAdresssBean s : strings) {
            if (s.getLevel().equals("2")) {
                arry_years.add(s);
            }
        }
        return arry_years;
    }

    private int forsize(ArrayList<MembersAdresssBean> arry_years, String content) {
        for (int i = 0; i < arry_years.size(); i++) {
            if (arry_years.get(i).getArea_code().equals(content)) {
                return i;
            }
        }
        return 0;
    }

    public <T extends View> T findView(View view, int viewId) {
        return (T) view.findViewById(viewId);
    }

    public void setOnTextLinstener(OnTextListLinstener listener) {
        this.cardListener = listener;
    }

    /**
     * 密码输入完回调接口
     */
    public interface OnTextListLinstener {
        /**
         * 加载更多数据回调方法，由组件自身触发
         */
        void OnDateText(String id, String text, String id2, String text2);
    }

    public void showPopupWindow(View view, String code1, String code2) {
        if (!this.isShowing()) {
            i1 = forsize(arry_years, code1);
            i2 = forsize(arry_list2, code2);
            wheel1.setDefault(i1);
            wheel2.setDefault(i2);
            wheel2.refreshData(stringArrayListString2(arry_list_z, arry_years.get(i1).getArea_code()));
            //获取屏幕信息
            WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
            lp.alpha = 0.6f; // 0.0-1.0
            activity.getWindow().setAttributes(lp);
            // 以下拉方式显示popupwindow
            this.showAtLocation(view,
                    Gravity.BOTTOM, 0, 0);
        }
    }
}

